<?php

namespace App\Models\Traits;

use App\Models\Reply;
use App\Models\Topic;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

trait ActiveUserCounter {
    protected $users = [];
    protected $topic_weight = 4;
    protected $reply_weight = 1;
    protected $pass_days = 7;
    protected $user_number = 6;
    //缓存
    protected $cache_key = 'bbschat_active_users';
    protected $cache_expire_in_seconds = 65 * 60;

    //计算每个人帖子近7填的发帖总数
    private function countTopicScore() {
        $user_topics = Topic::query()->select(DB::raw('user_id,count(*) as topic_count'))
            ->where('created_at', '>=', Carbon::now()->subDays($this->pass_days))
            ->groupBy('user_id')
            ->get();
        foreach ($user_topics as $value) {
            $this->users[$value->user_id]['score'] = $value->topic_count * $this->topic_weight;
        }
    }

    private function countReplyScore() {
        $user_replies = Reply::query()->select(DB::raw('user_id,count(*) as reply_count'))
            ->where('created_at', '>=', Carbon::now()->subDays($this->pass_days))
            ->groupBy('user_id')
            ->get();
        foreach ($user_replies as $value) {
            $reply_score = $value->reply_count * $this->reply_weight;
            if(isset($this->users[$value->user_id])){
                $this->users[$value->user_id]['score'] += $reply_score;
            }else{
                $this->users[$value->user_id]['score'] = $reply_score;
            }
        }
        return $this->users;
    }
    public function getCountedUsersCollection(){
        $this->countTopicScore();
        $this->countReplyScore();
        //排序数组
        $users = Arr::sort($this->users,function($user){
            return $user['score'];
        });
        $users = array_reverse($users,true);
        $users = array_slice($users,0,$this->user_number,true);
        $active_users = collect();
        foreach ($users as $user_id=>$user){
            $user = $this->find($user_id);
            if($user){
                $active_users->push($user);
            }
        }
        return $active_users ;
    }
    //将活跃用户写入缓存
    public function cacheActiveUsers($active_users){
        Cache::put($this->cache_key,$active_users,$this->cache_expire_in_seconds);
    }
    //计算并缓存活跃用户列表数据
    public function countAndCacheActiveUsers(){
        $active_users = $this->getCountedUsersCollection();
        $this->cacheActiveUsers($active_users);
    }
    //获取活跃用户列表
    public function getActiveUsers(){
        return Cache::remember($this->cache_key,$this->cache_expire_in_seconds,function (){
           return $this->getCountedUsersCollection();
        });
    }

}
